package com.ruoyi.app.controller.activityreg;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.app.domain.AppActivity;
import com.ruoyi.app.domain.AppCreditRules;
import com.ruoyi.app.mapper.AppActivityRegMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.domain.AppActivityReg;
import com.ruoyi.app.service.IAppActivityRegService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动报名Controller
 * 
 * @author ruoyi
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/system/reg")
public class AppActivityRegController extends BaseController
{
    @Autowired
    private IAppActivityRegService appActivityRegService;
    @Autowired
    private AppActivityRegMapper appActivityRegMapper;

    @GetMapping("/totalForStudent")
    public AjaxResult totalForStudent(HttpServletResponse response, AppActivityReg appActivityReg)
    {
        MPJLambdaWrapper<AppActivityReg> lwq = new MPJLambdaWrapper<AppActivityReg>()
                .selectAll(AppActivityReg.class)
                .select(AppCreditRules::getPoints)
                .leftJoin(AppActivity.class,AppActivity::getActivityId,AppActivityReg::getActivityId)
                .leftJoin(AppCreditRules.class,AppCreditRules::getRuleId,AppActivity::getRuleId)
                .eq(AppActivityReg::getUserId,SecurityUtils.getLoginUser().getUser().getUserId())
                .eq(AppActivityReg::getStatus,"1");
                ;
        List<AppActivityReg> dtos = appActivityRegMapper.selectJoinList(AppActivityReg.class,lwq);
        return AjaxResult.success(dtos.stream().map(x->x.getPoints()).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
    }


    @GetMapping("/listForStudent")
    public TableDataInfo listForStudent(AppActivityReg appActivityReg)
    {
        startPage();
        appActivityReg.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        List<AppActivityReg> list = appActivityRegService.selectAppActivityRegList(appActivityReg);
        return getDataTable(list);
    }

    @PostMapping("/exportForStudent")
    public void exportForStudent(HttpServletResponse response, AppActivityReg appActivityReg)
    {
        appActivityReg.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        List<AppActivityReg> list = appActivityRegService.selectAppActivityRegList(appActivityReg);
        ExcelUtil<AppActivityReg> util = new ExcelUtil<AppActivityReg>(AppActivityReg.class);
        util.exportExcel(response, list, "活动报名数据");
    }
    /**
     * 查询活动报名列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppActivityReg appActivityReg)
    {
        startPage();
        List<AppActivityReg> list = appActivityRegService.selectAppActivityRegList(appActivityReg);
        return getDataTable(list);
    }

    /**
     * 导出活动报名列表
     */
    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppActivityReg appActivityReg)
    {
        List<AppActivityReg> list = appActivityRegService.selectAppActivityRegList(appActivityReg);
        ExcelUtil<AppActivityReg> util = new ExcelUtil<AppActivityReg>(AppActivityReg.class);
        util.exportExcel(response, list, "活动报名数据");
    }

    /**
     * 获取活动报名详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appActivityRegService.selectAppActivityRegById(id));
    }

    /**
     * 新增活动报名
     */
    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppActivityReg appActivityReg)
    {
        return toAjax(appActivityRegService.insertAppActivityReg(appActivityReg));
    }

    /**
     * 修改活动报名
     */
    @Log(title = "活动报名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppActivityReg appActivityReg)
    {
        return toAjax(appActivityRegService.updateAppActivityReg(appActivityReg));
    }

    /**
     * 删除活动报名
     */
    @Log(title = "活动报名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appActivityRegService.deleteAppActivityRegByIds(ids));
    }

    @Log(title = "活动报名", businessType = BusinessType.INSERT)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody AppActivityReg appActivityReg)
    {
        return toAjax(appActivityRegService.submitAppActivityReg(appActivityReg));
    }

    @Log(title = "活动信息", businessType = BusinessType.UPDATE)
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody AppActivityReg appActivityReg)
    {
        return toAjax(appActivityRegService.auditAppActivityReg(appActivityReg));
    }
}
